﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Baza.EF.DAL;
using Baza.EF.BLL;

namespace Pep_3014_3070_Desktop
{
    public partial class ChangeDataOwner : Form
    {
        public ChangeDataOwner()
        {
            InitializeComponent();
        }

        private void ChangeDataOwners_Load(object sender, EventArgs e)
        {
            cmbIznajmljivac.DataSource = DAIznajmljivaciApartmani.SelectOwners();
            cmbIznajmljivac.DisplayMember = "FirstName";

        }



        private void cmbIznajmljivac_Format(object sender, ListControlConvertEventArgs e)
        {
            string lastname = ((Owners)e.ListItem).FirstName;
            string firstname = ((Owners)e.ListItem).LastName;
            e.Value = lastname + " " + firstname;

        }

        private void cmbIznajmljivac_SelectedIndexChanged(object sender, EventArgs e)
        {
            Owners owner = (Owners)cmbIznajmljivac.SelectedItem;

            imeInput.Text = owner.FirstName;
            prezimeInput.Text = owner.LastName;
            emailInput.Text = owner.Email;
            cmbGrad.DataSource = DAIznajmljivaciApartmani.SelectCities();
            cmbGrad.DisplayMember = "Name";
            cmbGrad.SelectedIndex = (int)owner.Address.CityID;

            BrojUliceInput.Text = owner.Address.StreetNumber.ToString();
            Adresa1Input.Text = owner.Address.Address1.ToString();
            Adresa2Input.Text = owner.Address.Address2.ToString();

            TextBoxMobilni.Text = owner.CellNumber;
            TextBoxPoslovni.Text = owner.BusinessNumber;
            TextBoxFiksni.Text = owner.HomeNumber;
            TextBoxFax.Text = owner.FaxNumber;

            textBoxWebSite.Text = owner.WebsiteURL;
            TextBoxInformacije.Text = owner.AdditionalContactInfo;



        }

        private void btnOdustani_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSpremi_Click(object sender, EventArgs e)
        {
            Owners owner = new Owners();

            // Countries country = (Countries)drzavaCmb.SelectedItem;

            owner.FirstName = imeInput.Text.Trim();
            owner.LastName = prezimeInput.Text.Trim();
            owner.Email = emailInput.Text.Trim();
            owner.CellNumber = TextBoxMobilni.Text;
            owner.BusinessNumber = TextBoxPoslovni.Text;
            owner.HomeNumber = TextBoxFiksni.Text;
            owner.FaxNumber = TextBoxFiksni.Text;
            owner.AdditionalContactInfo = TextBoxInformacije.Text;
            owner.WebsiteURL = textBoxWebSite.Text;


            if (passwordInput.Text.Length > 0)
            {
                owner.PasswordSalt = UIHelper.GenerateSalt();
                owner.PasswordHash = UIHelper.GenerateHash(passwordInput.Text, owner.PasswordSalt);

            }


            DAIznajmljivaciApartmani.UpdateOwners(owner);

        }
    }
}
